/* ====================================================================
 Small program for transfering a byte to a 8-bit shift register 
 Easaxen.com 2013
 ====================================================================*/

int clockPin = 12;
int dataPin = 11;
int latchPin = 8;

void setup() {  
  Serial.begin(9600);
  pinMode(clockPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  pinMode(latchPin,OUTPUT);
}

void sendByte(byte b8){
    byte tmp = 0b10000000; // Start on number 128  
    Serial.println(tmp,BIN);
    for(int j=0;j<8;j++){
      if ((b8 & tmp) == tmp){
        digitalWrite(dataPin,HIGH);
        Serial.print("1"); //Serial monitor information
      }
      else{
        digitalWrite(dataPin,LOW);
        Serial.print("0"); //Serial monitor information
      }  
       tmp = tmp >> 1;
       digitalWrite(clockPin,HIGH);
         delay(10); //Can be reduced further
       digitalWrite(clockPin,LOW);
       
    }
}

void loop() {
  //Alternate between displaying two numbers in binary. 
  
  //Example: defining which pins to set high by binary number
  Serial.println("-Begin of send-"); //Serial monitor information
  digitalWrite(latchPin,LOW); //Keep low until new byte is stored
    byte pins = 0b01010101;
    sendByte(pins);
  digitalWrite(latchPin,HIGH); //Moves byte from shift register to storage register
  Serial.println("-end of send-"); //Serial monitor information
  delay(2000);
  
  //Example when defining which pins to set high by decimal number
  Serial.println("-Begin of send-"); //Serial monitor information
  digitalWrite(latchPin,LOW);
    sendByte((byte)170);
  digitalWrite(latchPin,HIGH);
  Serial.println("-end of send-"); //Serial monitor information
  delay(2000);
  
}
